import { ALL, Body, Inject, Post, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController, BaseService } from 'midwayjs-cool-core';
import { Context } from 'vm';
import { classgroupStudentsEntity } from '../../../classgroupStudents/entity/classgroupStudents';
import { ClassroomEntity } from '../../../classroom/entity/classroom';
import { CourseTypeEntity } from '../../../courseType/entity/courseType';
import { classgroupEntity } from '../../entity/classgroup';
import { ClassGroupService } from '../../service/classgroup';

/**
 * 描述
 */
@Provide()
@CoolController({
  api: ['list'],
  // api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: classgroupEntity,
  service:ClassGroupService,
  before: ctx => {
    // 将前端的数据转JSON格式存数据库
    // const { data } = ctx.request.body;
    let data = ctx.request.body;

    ctx.request.body.data = JSON.stringify(data);
  },
  pageQueryOp:{ 
    // 让title字段支持模糊查询
    keyWordLikeFields: ['a.class_name'],
    // 让type字段支持筛选
    fieldEq: ['class_number'],
    // 指定返回字段
    select: ['a.*', 'b.*'],
    // // 关联表用户表
    leftJoin: [{
      entity: classgroupStudentsEntity,
      alias: 'b',
      condition: 'a.id = b.classgroup_id'
    }],
    where: async (ctx: Context) => { 
      return [ 
        ['a.isDelete = :isDelete', { isDelete: false }] 
      ] 
    } 
  },
  listQueryOp:{ 
    // 让type字段支持筛选
    fieldEq: ['b.student_id','a.class_date','a.class_course_type'],
    // 指定返回字段
    select: ['a.*','b.student_id','c.name AS classroomName','d.course_id'],
    // // 关联表用户表
    leftJoin: [{
      entity: classgroupStudentsEntity,
      alias: 'b',
      condition: 'a.id = b.classgroup_id'
    },{
      entity: ClassroomEntity,
      alias: 'c',
      condition: 'a.class_class_classroom = c.id'
    },{
      entity: CourseTypeEntity,
      alias: 'd',
      condition: 'a.class_course_type = d.id'
    }],
    where: async (ctx: Context) => { 
      const body_data: any= JSON.parse(ctx.request.body.data)
      console.log(body_data,'111111111');
      let ret:any = [
        ['a.isDelete = :isDelete', { isDelete: false }]
      ]
      if (body_data.student_id){
        ret.push(['b.student_id = :studentId', { studentId: body_data.student_id }])
      }
      if (body_data.class_course_type){
        ret.push(['a.class_course_type = :class_course_type', { class_course_type: body_data.class_course_type }])
      }
      if (body_data.class_date){
        ret.push(['a.class_date = :class_date', { class_date: body_data.class_date }])
      }
      return ret
    } 
  }
})
export class classgroupAppController extends BaseController {
  @Inject()
  service:BaseService

  @Post('/classSchedule')
  async classCourseStudents(@Body(ALL) queryObject: any) {
    // 请求体中获取学生id
    const {class_date} = queryObject
    
    const info = await this.service.nativeQuery(
      `
      select a.class_week, group_concat(a.class_start_time,'-',a.class_end_time,'+',d.name,'_',c.name,'&',a.class_date,a.class_name,a.class_number,'&',b.studentNames) as classInfo from classgroup a 
        left join(select classgroup_id ,group_concat(b.name Separator '-') as studentNames from classgroup_students a
        left join student b on a.student_id = b.id
        group by classgroup_id) b on a.id = b.classgroup_id
        left join teacher c on a.class_teacher = c.id
        left join classroom d on a.class_class_classroom = d.id
        where a.class_date = '${class_date}' and a.isDelete = false
        group by a.class_week
        order by a.class_week asc
      `
    )
    // const info = await this.service.nativeQuery(
    //   `
    //   select class_week,group_concat(class_start_time,class_end_time) from classgroup group by class_week
    //   `
    // )
    
    return {code:1000,data:info}
  }

  @Post('/classWeek')
  async classWeek(@Body(ALL) queryObject: any) {
    // 请求体中获取学生id
    const {class_date} = queryObject
    
    const info = await this.service.nativeQuery(
      `
      select distinct class_week as classWeek from classgroup 
      where class_date = '${class_date}' and isDelete = false
      order by class_week asc
      `
    )
    // const info = await this.service.nativeQuery(
    //   `
    //   select class_week,group_concat(class_start_time,class_end_time) from classgroup group by class_week
    //   `
    // )
    
    return {code:1000,data:info}
  }
  
  @Post('/classScheduleClassWeek')
  async classScheduleClassWeek(@Body(ALL) queryObject: any) {
    // 请求体中获取学生id
    const {class_date} = queryObject
    
    const info = await this.service.nativeQuery(
      `
      select concat(a.class_start_time,'-',a.class_end_time) as class_week, group_concat(a.class_week,'+',d.name,'_',c.name,'&',a.class_date,a.class_name,a.class_number,'&',b.studentNames) as classInfo from classgroup a 
        left join(select classgroup_id ,group_concat(b.name Separator '-') as studentNames from classgroup_students a
        left join student b on a.student_id = b.id
        group by classgroup_id) b on a.id = b.classgroup_id
        left join teacher c on a.class_teacher = c.id
        left join classroom d on a.class_class_classroom = d.id
        where a.class_date = '${class_date}' and a.isDelete = false
        group by a.class_start_time,a.class_end_time
      `
    )
    // const info = await this.service.nativeQuery(
    //   `
    //   select class_week,group_concat(class_start_time,class_end_time) from classgroup group by class_week
    //   `
    // )
    
    return {code:1000,data:info}
  }

  @Post('/classTime')
  async classTime(@Body(ALL) queryObject: any) {
    // 请求体中获取学生id
    const {class_date} = queryObject
    
    const info = await this.service.nativeQuery(
      `
      select concat(class_start_time,'-',class_end_time) as classTime from classgroup 
      where class_date = '${class_date}' 
      group by class_start_time,class_end_time
      order by class_start_time asc
      `
    )
    // const info = await this.service.nativeQuery(
    //   `
    //   select class_week,group_concat(class_start_time,class_end_time) from classgroup group by class_week
    //   `
    // )
    
    return {code:1000,data:info}
  }

}
